package com.winspread.app.appbarlayoutui

import com.google.android.material.appbar.AppBarLayout
import com.winspread.active.extension.i
import com.winspread.app.R
import com.winspread.app.databinding.ActivityAppbarlayoutBinding
import com.winspread.mvvm.base.vm.BaseNotVmActivity

class AppBarLayoutActivity : BaseNotVmActivity<ActivityAppbarlayoutBinding>(R.layout.activity_appbarlayout) {

    var expanded: AppBarStateChangeListener.State? = AppBarStateChangeListener.State.IDLE

    override fun initView() {
        binding.appbarlayout.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout?, state: State?, verticalOffset: Int) {
                expanded = state
            }

            override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                super.onOffsetChanged(appBarLayout, verticalOffset)
                val totalScrollRange = appBarLayout!!.totalScrollRange
                val fl = -verticalOffset / totalScrollRange.toFloat()
                "initView: $fl".i()
                binding.car.alpha = fl
                binding.car.translationX = fl * 30
                binding.rl.scaleX = 1 - fl
                binding.rl.scaleY = 1 - fl
                binding.rl.translationY = fl * 400

            }
        })
        binding.car.setOnClickListener {
            if (expanded == AppBarStateChangeListener.State.EXPANDED) {
                binding.appbarlayout.setExpanded(false, true)
            } else if (expanded == AppBarStateChangeListener.State.COLLAPSED) {
                binding.appbarlayout.setExpanded(true, true)
            }
        }
    }


}


